import axios from 'axios'
import qs from 'qs'
import {
	ElMessage
} from 'element-plus'
import CryptoJS from "crypto-js";
import store from '../store/index'
import util from './util'

const CONFIG = {
  // 开发环境配置
  development: {
    // baseUrl: 'http://192.168.3.55:8081',
    baseUrl: 'https://www.jiangxiyiren.cn/cdz-java',
    // baseUrl: 'http://192.168.3.37:9110',
    // baseUrl: 'http://192.168.0.150:9110',
    // baseUrl: 'http://49.234.107.154:9110',
    // baseUrl: 'http://192.168.3.38:9110',
  },
  // 生产环境配置
  production: {
    // baseUrl: 'https://www.king2.cn/cdz-java',
	baseUrl: 'https://www.jiangxiyiren.cn/cdz-java',
  }
};


const apiUrl = CONFIG[process.env.NODE_ENV].baseUrl;
// 创建 axios 实例
const request = axios.create({
	baseURL: apiUrl,
	timeout: 60000, // 请求超时时间
	headers: {
		'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
	}
})

// 异常拦截处理器
const errorHandler = (error) => {
	if (error.data.msg) {
		ElMessage.error(error.data.msg)
	} else {
		ElMessage.error(error.toString())
	}
	return Promise.reject(error)
}

// 请求拦截器
request.interceptors.request.use(config => {
	// 如果 token 存在  让每个请求携带自定义 token 请根据实际情况自行修改
	const token = localStorage.getItem('accesstoken')
	if (token) {
		config.headers['axh-access-token'] = token
	}
	return config
}, errorHandler)

// 响应拦截器
request.interceptors.response.use((response) => {
	if (response && response.data && response.data.code) {
		if (response.data.code === 200) {
			// 判断是否需要进行解密
			if (response.data.ase) {
				// 对Key进行排序
				let aesKey = util.sort_ascii(response.data.aesKey);
				// 进行解密
				let aesKeyTemp = CryptoJS.enc.Utf8.parse(aesKey);
				var decrypt = CryptoJS.AES.decrypt(response.data.aesContent, aesKeyTemp, {
					mode: CryptoJS.mode.ECB,
					padding: CryptoJS.pad.Pkcs7
				});
				return JSON.parse(CryptoJS.enc.Utf8.stringify(decrypt).toString())
			}
			return response.data
		} else if (response.data.code === 410) {
			ElMessage.error(response.data.msg);
			// 查询菜单信息
			localStorage.removeItem('accesstoken')
			localStorage.removeItem('dtUsersVo')
			store.commit('clearVisitedTags')
			location.reload()
			return Promise.reject(response.data)
		} else if (response.data.code === 100) {
			ElMessage.error(response.data.msg);
			return Promise.reject(response.data)
		} else {
			ElMessage.error(response.data.msg);
		}
	} else {
		ElMessage.error('系统错误，请稍后重试！');
	}
}, errorHandler)


export default {
	post(url, data, headers) {
		let params = data
		// 不是formdata类型
		if (Object.prototype.toString.call(params) != '[object FormData]') {
			params = qs.stringify(data)
		}
		return new Promise((resolve, reject) => {
			request({
				method: 'post',
				url,
				data: params,
				headers: headers
			})
				.then(res => {
					resolve(res)
				})
				.catch(err => {
					reject(err)
				});
		})
	},

	get(url, data) {
		return new Promise((resolve, reject) => {
			request({
				method: 'get',
				url,
				params: data,
			})
				.then(res => {
					resolve(res)
				})
				.catch(err => {
					reject(err)
				})
		})
	}, apiUrl
};